package com.xhwl.centiotdata.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
* @Description: 代码版本获取工具类
* @Author: lixy
* @CreateDate: 2022/2/15 16:09
*/
@Component
public class VersionManageConfig {
   private static String productCode, versionCode, periodVersion;

   @Value("${version.product-code}")
   private String productCode1;

   @Value("${version.version-code}")
   private String versionCode1;

   @Value("${version.period-version}")
   private String periodVersion1;

   @PostConstruct
   public void getValue() {
       productCode = this.productCode1;
       versionCode = this.versionCode1;
       periodVersion = this.periodVersion1;
   }

   /**
    * 获取上次提交代码时间
    * @return 提交日期
    */
//    public static String lastCommitDate() {
//        int commiTime = 0;
//        try {
//            Repository repository = new FileRepositoryBuilder()
//                    .setGitDir(Paths.get(new File("").getCanonicalPath(), ".git").toFile())
//                    .build();//"D:\\develop\\IdeaProjects\\cent-upgrade"
//            Git git = new Git(repository);
//            Iterable<RevCommit> commits = git.log().call();
//            for (RevCommit commit : commits) {
//                commiTime = commit.getCommitTime();
//                break;
//            }
//        } catch (IOException | GitAPIException e) {
//            e.printStackTrace();
//        }
//        long nowTimeLong = (long) commiTime * 1000;
//        DateFormat sdf = new SimpleDateFormat("yyyyMMdd");
//        return sdf.format(nowTimeLong);
//    }
//
   public static String currentDate(){
       DateFormat sdf = new SimpleDateFormat("yyyyMMdd");
       return sdf.format(new Date());
   }

   /**
    * 获取代码版本
    * @return 代码版本
    */
   public static String currentVersion() {
       return productCode + "_" + versionCode + "." + currentDate() + periodVersion;
   }
}
